package com.nmgsr.ptmsapp.activity;


import static com.nmgsr.ptmsapp.utils.ContantConfig.Base_URL;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.nmgsr.nettools.base.BaseActivity;
import com.nmgsr.nettools.util.LogUtil;
import com.nmgsr.nettools.util.ToastUtil;
import com.nmgsr.ptmsapp.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * 浏览图片
 */
public class ImageActivity extends BaseActivity {
    private static final String TAG = ImageActivity.class.getSimpleName();
    public static final String IMAGES = "images";
    public static final String POSITION = "position";
    public static final String ISLOCALPHOTO = "isLocalPhoto";

    private TextView textview;
    private ViewPager viewpager;
    private MyImageAdapter adapter;

    private ArrayList<String> list;
    private int currentPosition;
    private boolean isLocalPhoto;
    String Base_OSS_URL;
    public static void startActivity(Context context, ArrayList<String> list, int position) {
        Intent intent = new Intent(context, ImageActivity.class);
        intent.putStringArrayListExtra(IMAGES, list);
        intent.putExtra(POSITION, position);
        context.startActivity(intent);
    }

    //isLocalPhoto: 打开的是否是本地图片
    public static void startActivity(Context context, ArrayList<String> list, int position, boolean isLocalPhoto) {
        Intent intent = new Intent(context, ImageActivity.class);
        intent.putStringArrayListExtra(IMAGES, list);
        intent.putExtra(POSITION, position);
        intent.putExtra(ISLOCALPHOTO, isLocalPhoto);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.image_activity;
    }

    @Override
    protected void initView() {
        LogUtil.e("=======> " + TAG);
        view_status_bar.setVisibility(View.GONE);
        fl_actionbar.setVisibility(View.GONE);
        mContext = ImageActivity.this;

        viewpager = findViewById(R.id.viewpager);
        textview = findViewById(R.id.textview);
    }

    @Override
    protected void initData() {
        list = getIntent().getStringArrayListExtra(IMAGES);
        currentPosition = getIntent().getIntExtra(POSITION, 0);
        isLocalPhoto = getIntent().getBooleanExtra(ISLOCALPHOTO, false);
        if (list == null) {
            ToastUtil.showToast("未获取到图片数据，请重试");
            return;
        }
        adapter = new MyImageAdapter(list, this);
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(currentPosition, false);
        textview.setText(currentPosition + 1 + "/" + list.size());
        viewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPosition = position;
                textview.setText(position + 1 + "/" + list.size());
            }
        });
    }


    class MyImageAdapter extends PagerAdapter {
        private List<String> images;
        private Context context;

        public MyImageAdapter(List<String> images, Context context) {
            this.images = images;
            this.context = context;
        }

        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position) {
            PhotoView photoView = new PhotoView(context);
            photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(ImageView view, float x, float y) {
                    finish();
                }
            });

            String allUrl = images.get(position);
            if (isLocalPhoto) {
                Picasso.get().load(new File(allUrl)).into(photoView);
            } else {

                //String imgUrl = Base_OSS_URL + allUrl;
                String imgUrl =Base_URL+allUrl;
                LogUtil.e("=======> imgUrl= " + imgUrl);
                Picasso.get().load(imgUrl).placeholder(R.drawable.no_data).error(R.drawable.error_data).into(photoView);
            }
            try {
                viewGroup.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return photoView;
        }


        @Override
        public int getCount() {
            return images != null ? images.size() : 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }


}
